package com.cy.dda.service.collection.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.dao.mapper.collection.CollectionRecordAttachmentMapper;
import com.cy.dda.dao.mapper.collection.RepayRecordMapper;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.domain.collection.RepayRecord;
import com.cy.dda.model.query.collection.RepayRecordListAdminQuery;
import com.cy.dda.model.query.collection.RepayRecordListQuery;
import com.cy.dda.model.vo.collection.CollectionRecordAttachmentVO;
import com.cy.dda.model.vo.collection.CollectionRecordDetailInfoAdminVO;
import com.cy.dda.model.vo.collection.RepayRecordListAdminVO;
import com.cy.dda.model.vo.collection.RepayRecordListVO;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.collection.RepayRecordService;

/**
 * 催收回款记录 RepayRecordService
 *
 * @author: tanyilin
 * @create: 2018年11月6日 上午10:27:16
 */
@Service
public class RepayRecordServiceImpl extends BaseService implements RepayRecordService {

	@Autowired
	private RepayRecordMapper repayRecordMapper;
	@Autowired
	private CollectionRecordAttachmentMapper collectionRecordAttachmentMapper;

	@Override
	public PageBean<RepayRecordListAdminVO> selectRepayRecordListAdmin(RepayRecordListAdminQuery query) {
		
		if(query.getRepayStartTime() != null) {
            query.setRepayStartTime(CommonUtils.getDayStartTime(query.getRepayStartTime()));
        }
        if(query.getRepayEndTime() != null) {
            query.setRepayEndTime(CommonUtils.getDayEndTime(query.getRepayEndTime()));
        }
		
		int total = repayRecordMapper.selectRepayRecordListAdminCount(query);
		List<RepayRecordListAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = repayRecordMapper.selectRepayRecordListAdmin(query);
			list.stream().forEach(r -> {
				r.setCasesTypeText(r.getCasesType().getText());
				r.setCasesStatusText(r.getCasesStatus().getText());
				r.setRepayTypeText(r.getRepayType().getText());
				r.setRepayWayText(r.getRepayWay().getText());
				r.setRepayStatusText(r.getRepayStatus().getText());
			});
		}
		
		return new PageBean<RepayRecordListAdminVO>(list, total);
	}

	@Override
	@Transactional
	public void updateRepayRecord(RepayRecord repayRecord) {
		repayRecordMapper.updateByPrimaryKeySelective(repayRecord);
	}

	@Override
	public CollectionRecordDetailInfoAdminVO selectRepayRecordDetailInfoAdminVO(Long repayRecordId) {
		
		CollectionRecordDetailInfoAdminVO modelVo = repayRecordMapper.selectRepayRecordDetailInfoAdminVO(repayRecordId);
		state(modelVo != null, "回款信息异常");
		modelVo.setCollectionRecordTypeText(modelVo.getCollectionRecordType().getText());
		modelVo.setRepayTypeText(modelVo.getRepayType().getText());
		modelVo.setRepayWayText(modelVo.getRepayWay().getText());
		
		//查询该回款信息对应的催收记录的上传的附件信息
		Long collectionRecordId = modelVo.getCollectionRecordId();
		List<CollectionRecordAttachmentVO> attachmentList = collectionRecordAttachmentMapper.selectAttachmentBycollectionRecordId(collectionRecordId);
		if(!CommonUtils.isEmpty(attachmentList)) {
			modelVo.setAttachmentList(attachmentList);
		}
		
		return modelVo;
	}

	@Override
	public PageBean<RepayRecordListVO> selectRepayRecordList(RepayRecordListQuery query) {
		int total = repayRecordMapper.selectRepayRecordListCount(query);
		List<RepayRecordListVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = repayRecordMapper.selectRepayRecordList(query);
			list.stream().forEach(r -> {
				r.setRepayTypeText(r.getRepayType().getText());
				r.setRepayStatusText(r.getRepayStatus().getText());
			});
		}
		
		return new PageBean<RepayRecordListVO>(list, total);
	}
	
}
